package com.itcodai.dbconnect.bean;

import com.itcodai.dbconnect.enums.DBTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author c-fanqj
 * @date 2023/3/29
 * @apiNote
 */
public class DBContextHolder {

    private static final Logger logger
            = LoggerFactory.getLogger(DBContextHolder.class);

    private static final ThreadLocal<DBTypeEnum> contextHolder
            = new ThreadLocal<>();

    private static final AtomicInteger counter
            = new AtomicInteger(-1);

    public static void set(DBTypeEnum dbType) {
        contextHolder.set(dbType);
    }

    public static DBTypeEnum get() {
        return contextHolder.get();
    }

    public static void master() {
        set(DBTypeEnum.MASTER);
        logger.info("切换到master");
    }

    public static void slave() { // 轮询
        int index = counter.getAndIncrement() % 2;
        if (counter.get() > 9999) {
            counter.set(-1);
        }
        if (index == 0) {
            set(DBTypeEnum.SLAVE1);
            logger.info("切换到slaver1");
        } else {
            set(DBTypeEnum.SLAVE2);
            logger.info("切换到slaver2");
        }
    }

}
